﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreesenseSubAIS.Core
{
    public class ProductManager
    {
        // 从 程序目录下的 PRODUCT 文件夹里面 
        public static List<string> GetExistProduct()
        {
            string path = Path.Combine(System.Windows.Forms.Application.StartupPath, "Projects");
            if (Directory.Exists(path))
            {
                string[] productList = Directory.GetDirectories(path);
                List<string> ls = new List<string>();
                foreach (string pa in productList)
                {
                    ls.Add(Path.GetFileName(pa));
                }
                return ls;
            }
            else
            {
                Directory.CreateDirectory(path);
                return null;
            }
        }

        public static ProjectParam OpenProduct(string fileName)// mark ： 打开这一产品的 第几种方案
        {
            string path = Path.Combine(System.Windows.Forms.Application.StartupPath, "Projects");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                return null;
            }
            path = Path.Combine(path, fileName);
            path = Path.Combine(path, "productconfig.cog");
            if (!System.IO.File.Exists(path))
            {
                return new ProjectParam(fileName);
            }
            try
            {
                var pro = BinarySerializeHelper.DeformatFile(path);
                return (ProjectParam)pro;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("参数打开失败！" + ex.TargetSite + ex.Message);
                return new ProjectParam(fileName);
            }
        }

        public static void NewProdect(string name)
        {
            string path = Path.Combine(System.Windows.Forms.Application.StartupPath, "Projects");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            Directory.CreateDirectory(Path.Combine(path, name));
            // Directory.CreateDirectory(Path.Combine(path, name,"MoudlImgs"));
        }

        public static void DeleteProduct(string name)
        {
            string path = Path.Combine(System.Windows.Forms.Application.StartupPath, "Projects");
            if (Directory.Exists(path))
            {
                Directory.Delete(Path.Combine(path, name), true);
            }
        }

        public static void SaveProduct(ProjectParam p)// mark ： 打开这一产品的 第几种方案
        {
            string path = Path.Combine(System.Windows.Forms.Application.StartupPath, string.Format("Projects\\{0}", p.ProductName));
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            try
            {
                BinarySerializeHelper.FormatFile(Path.Combine(path, "productconfig.cog"), p);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.TargetSite + ex.Message);
            }
        }
        public static void BackUpPara(ProjectParam p)
        {
            string path = Path.Combine(System.Windows.Forms.Application.StartupPath, string.Format("Projects\\{0}", p.ProductName));
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            try
            {
                BinarySerializeHelper.SaveToXML(Path.Combine(path, p.ProductName + ".xml"), p);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.TargetSite + ex.Message);
            }
        }
        public static ProjectParam RestoryPara(string productname, bool isold)
        {
            string path = Path.Combine(System.Windows.Forms.Application.StartupPath, "Projects");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                return null;
            }
            path = Path.Combine(path, productname);
            if (isold)
                productname += "_new";
            path = Path.Combine(path, productname + ".xml");
            if (!System.IO.File.Exists(path))
            {
                return new ProjectParam(productname);
            }
            try
            {
                var pro = BinarySerializeHelper.LoadFromXML(path, typeof(ProjectParam));
                return (ProjectParam)pro;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("参数打开失败！" + ex.TargetSite + ex.Message);
                return null;
            }
        }
        public static DeviceParameter  RestoryDevicePara(string paramname)
        {
            string path = Path.Combine(System.Windows.Forms.Application.StartupPath, paramname);

            if (!Directory.Exists(path))
            {
                return null;
            }
            path = Path.Combine(path, paramname + ".xml");
            if (!System.IO.File.Exists(path))
            {
                return new DeviceParameter();
            }
            try
            {
                var pro = BinarySerializeHelper.LoadFromXML(path, typeof(DeviceParameter));
                return (DeviceParameter)pro;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("硬件参数恢复失败！" + ex.TargetSite + ex.Message);
                return null;
            }
        }
        public static void ReNameProduct(string oldname, string newname)
        {
            string path = Path.Combine(System.Windows.Forms.Application.StartupPath, "Projects");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                return;
            }
            string oldpath = Path.Combine(path, oldname);

            if (Directory.Exists(oldpath))
            {
                ProjectParam onp = OpenProduct(oldpath);
                onp.ProductName = newname;
                Directory.Move(oldpath, Path.Combine(path, newname));

                SaveProduct(onp);
                //string newpath = ;

            }
        }

        public static ProjectParam CloneProduct(ProjectParam p)
        {
            ProjectParam tempP = null;
            if (p != null)
            {
                MemoryStream ms = BinarySerializeHelper.FormatFile(p);
                ms.Position = 0;
                tempP = (ProjectParam)BinarySerializeHelper.DeformatFile(ms);
                ms.Close();
            }

            return tempP;
        }
    }
}
